package core

import (
	"context"

	"github.com/go-kratos/kratos/contrib/registry/etcd/v2"
	"github.com/go-kratos/kratos/v2/selector"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	rpc "google.golang.org/grpc"
)

func InitGRPCConn(
	serviceName string,
	r *etcd.Registry,
	filter selector.NodeFilter,
) (*rpc.ClientConn, error) {
	return grpc.DialInsecure(
		context.Background(),
		grpc.WithEndpoint("discovery:///"+serviceName),
		grpc.WithDiscovery(r),
		grpc.WithHealthCheck(true),
		grpc.WithNodeFilter(filter),
	)
}

// func aaa() {



// 连接到 gRPC 服务器
	// conn, err := grpc.NewClient(
	// 	"grpc://192.168.11.113:9620",
	// 	grpc.WithTransportCredentials(insecure.NewCredentials()),
	// )
// if err != nil {
// 	log.Fatalf("did not connect: %v", err)
// }
// defer conn.Close()
// client := pbPermission.NewPermissionClient(conn)
// md := metadata.Pairs("Authorization", "2d485607-8d1c-442b-9550-16ef5e1e2461")
// ctx := metadata.NewOutgoingContext(context.Background(), md)
// rsp, err := client.ListPermission(ctx, nil)
// if err != nil {
// 	log.Fatalf("could not greet: %v", err)
// }
// println(rsp.String())
// cli, err := clientv3.New(clientv3.Config{
// 	Endpoints:             []string{"192.168.11.112:2379", "192.168.11.113:2379"},
// })
// if err != nil {
// 	println(err.Error())
// }
// rsp, err := cli.Get(context.Background(), "/microservices/dango-auth", clientv3.WithPrefix())
// if err != nil {
// 	println(err.Error())
// }
// if len(rsp.Kvs) == 0 {
// 	println("service not found")
// } else {
// 	println(string(rsp.Kvs[0].Value))
// }
// cli.Close()
// }
